package com.example.memberships.vm

import androidx.lifecycle.MutableLiveData
import com.example.common.view.vm.BaseViewModel
import com.example.member.entity.MemberEntity
import com.example.member.entity.MembershipEntity
import com.example.member.model.MemberRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MemViewModel  @Inject constructor(private val repo: MemberRepository): BaseViewModel() {
    val mMemSuccess = MutableLiveData<MemberEntity>()
    fun getMember(id:Int){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getMember(id)
            if(code == 200){
                mMemSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mMemUpSuccess = MutableLiveData<MemberEntity>()
    fun getMemberUp(newMembershipLevel:String,id:Int){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getMemberUp(newMembershipLevel,id)
            if(code == 200){
                mMemUpSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mMemShipSuccess = MutableLiveData<List<MembershipEntity>>()
    fun getMemberShip(id:Int){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getMembership(id)
            if(code == 200){
                mMemShipSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
}